package com.boot.producer.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.boot.producer.entity.Track;
import com.boot.producer.service.TrackService;
import com.boot.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhangZhilei
 * @since 2019-02-22
 */
@RestController
@RequestMapping("/track")
@Api("行驶轨迹")
public class TrackController {
    @Autowired
    TrackService trackService;

    @ApiOperation("获取所有数据")
    @GetMapping("list")
    public List<Track> getAll(){
        return trackService.selectList(null);
    }
    @ApiOperation("插入一条数据")
    @PostMapping("insert")
    public void saveTrack(@RequestBody Track track){
        trackService.insert(track);
    }
    @ApiOperation("批量插入数据")
    @PostMapping("batchInsert")
    public void batchSave(@RequestBody List<Track> tracks){
        trackService.insertBatch(tracks);
    }
    @GetMapping("listByDate")
    @ApiOperation("根据时间获取行驶轨迹")
    public List<Track> listByDate(@RequestParam("date")Date date){
        Date start = DateUtil.getStartOfADay(date);
        Date end = DateUtil.getEndOfADay(date);
        return trackService.selectList(new EntityWrapper<Track>().between("time",start,end));
    }
    @PostMapping("deleteBatch")
    @ApiOperation("批量删除")
    public void deleteBatch(@RequestBody List<Track> tracks){
        for (Track track:tracks){

            trackService.deleteById(track);
        }
    }

}

